﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Toolbox.util;

namespace Toolbox.form.box
{
    public partial class BinaryConvertForm : UIPage
    {
        public BinaryConvertForm()
        {
            InitializeComponent();
        }

        private void BinaryConvertForm_Load(object sender, EventArgs e)
        {
            // 页面加载完成
            int i = 1;

            // 将int 转换到数组中，然后根据数组的地址进行判断
            // 如果是小端模式，bytes={1,0,0,0}
            byte[] bytes = BitConverter.GetBytes(i);

            Console.WriteLine("=======================");
            Console.WriteLine(bytes[0].ToString("X2"));
            Console.WriteLine(bytes[1].ToString("X2"));
            Console.WriteLine(bytes[2].ToString("X2"));
            Console.WriteLine(bytes[3].ToString("X2"));
            Console.WriteLine("=======================");

            string str = "小端和大端表示多个字节值的哪一端存储在该值的起始地址。";
            if (bytes[0] == 1)
            {
                uiMarkLabel1.Text = "当前电脑：小端模式  " + str;
            }
            else
            {
                uiMarkLabel1.Text = "当前电脑：大端模式  " + str;
            }

            string line0 = "Long(Float) AB CD" + System.Environment.NewLine;
            string line1 = "Long(Float) CD AB" + System.Environment.NewLine;
            string line2 = "Long(Float) BA DC" + System.Environment.NewLine;
            string line3 = "Long(Float) DC BA" + System.Environment.NewLine;

            uiTextBox1.Text = string.Format("{0}{1}{2}{3}", line0, line1, line2, line3);
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            // 进制转换按钮
            string inputStr = uiTextBox2.Text.Replace(" ", "");
            if (inputStr.Length < 1)
            {
                uiTextBox3.Clear();
                return;
            }
            if (uiRadioButton1.Checked)
            {
                // 10进制

                if (uiRadioButton3.Checked)
                {
                    // 转10进制
                    uiTextBox3.Text = inputStr;
                }
                else if (uiRadioButton4.Checked)
                {
                    // 转16进制
                    int inputInt = int.Parse(inputStr);
                    uiTextBox3.Text = HexUtils.IntToHexStr(inputInt);
                }
            }
            else if (uiRadioButton2.Checked)
            {
                // 16进制
                if (uiRadioButton3.Checked)
                {
                    // 转10进制
                    uiTextBox3.Text = HexUtils.HexStrToInt(inputStr).ToString();
                }
                else if (uiRadioButton4.Checked)
                {
                    // 转16进制
                    uiTextBox3.Text = inputStr;
                }
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            // 单精度浮点数转换按钮
            string inputStr = uiTextBox4.Text.Replace(" ", "");
            if (inputStr.Length < 1)
            {
                uiTextBox5.Clear();
                return;
            }
            if (uiRadioButton5.Checked)
            {
                // 10进制

                if (uiRadioButton7.Checked)
                {
                    // 转10进制
                    uiTextBox5.Text = inputStr;
                }
                else if (uiRadioButton8.Checked)
                {
                    // 转16进制
                    float inputFloat = float.Parse(inputStr);
                    string hexStr = HexUtils.FloatToHexStr(inputFloat);
                    uiTextBox5.Text = HexUtils.spaceHexStr(hexStr);
                }
            }
            else if (uiRadioButton6.Checked)
            {
                // 16进制
                if (uiRadioButton7.Checked)
                {
                    // 转10进制
                    uiTextBox5.Text = HexUtils.HexStrToFloat(inputStr).ToString();
                }
                else if (uiRadioButton8.Checked)
                {
                    // 转16进制
                    uiTextBox5.Text = inputStr;
                }
            }
        }
    }
}
